package com.tarena.lbs.attach.controller;

import com.tarena.lbs.attach.entity.AttachFile;
import com.tarena.lbs.attach.service.FileService;
import com.tarena.lbs.attach.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/attach")
public class FileUploadController {

    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public ResponseEntity<AttachFile> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        // 校验文件
        FileUtil.validateFile(file);

        // 保存文件并返回文件信息
        AttachFile attachFile = fileService.saveFile(file);
        return ResponseEntity.ok(attachFile);
    }
}
